package threading;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-07-03
 * Time: 17:17
 */
public class ThreadDemo9 {//自己创建标志位
    public static boolean isQuit = false;//结束标志位，成员变量不受变量捕获的规则限制

    public static void main(String[] args) {
        boolean isQuit1 = false;//lambda受变量捕获的规则限制，只能是final或者实际final（就是没有修改过）
        Thread t = new Thread(() -> {
            while (!isQuit) {//!isQuit1
                System.out.println("Hello t");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("t线程终止");
        });

        t.start();

        //在主线程中，修改isQuit
        try {
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //isQuit1 = true;//会报错
        isQuit = true;
    }
}
